package com.yz.test.parking.getway;


import com.yz.test.parking.sign.SignatureHelper;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/yz/parking")
public class ParkingYZOpenGetway {

    @RequestMapping(value="/index",method= RequestMethod.GET)
    public String jumpIndexPage(){
        return "index";
    }

    @RequestMapping(value="/tradePay",method= RequestMethod.GET)
    public String jumpTradePayPage(){
        return "tradePay";
    }

    @RequestMapping(value="/tradeJsPay",method= RequestMethod.GET)
    public String jumpTradeJsPayPage(){
        return "tradeJsPay";
    }

    @RequestMapping(value="/tradeClose",method= RequestMethod.GET)
    public String jumpTradeClosePage(){
        return "tradeClose";
    }

    @RequestMapping(value="/tradeQuery",method= RequestMethod.GET)
    public String jumpTradeQueryPage(){
        return "tradeQuery";
    }

    @RequestMapping(value="/tradeRevoke",method= RequestMethod.GET)
    public String jumpTradeRevokePage(){
        return "tradeRevoke";
    }

    @RequestMapping(value="/tradeRefund",method= RequestMethod.GET)
    public String jumpTradeRefundPage(){
        return "tradeRefund";
    }

    @RequestMapping(value="/tradeRefundQuery",method= RequestMethod.GET)
    public String jumpTradeRefundQueryPage(){
        return "tradeRefundQuery";
    }

    @RequestMapping(value="/authOpenid",method= RequestMethod.GET)
    public String jumpAuthOpenidPage(){
        return "authOpenid";
    }

    @RequestMapping(value="/tradeDataDownload",method= RequestMethod.GET)
    public String jumpTradeDataDownloadPage(){
        return "tradeDataDownload";
    }

    @RequestMapping(value="/getTimestamp",method= RequestMethod.GET)
    @ResponseBody
    public String getTimestamp(HttpServletRequest req, HttpServletResponse resp){
        String timestampStr = System.currentTimeMillis()+"";
        String timestampJson = "{\"timestampStr\":\""+timestampStr+"\"}";
        return timestampJson;
    }

    @RequestMapping(value="/getOutTradeNo",method= RequestMethod.GET)
    @ResponseBody
    public String getOutTradeNo(HttpServletRequest req, HttpServletResponse resp){
        String timestampStr = System.currentTimeMillis()+"";
        String outTradeNoStr = "ISV"+timestampStr;
        String outTradeNoJson = "{\"outTradeNoStr\":\""+outTradeNoStr+"\"}";
        return outTradeNoJson;
    }

    @RequestMapping(value="/getSign",method= RequestMethod.GET)
    @ResponseBody
    public String getSing(HttpServletRequest req, HttpServletResponse resp){
        String privateKey ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMqnmT2/IbaX337ToGt+0y/hR816UEb9F3HUwCqSqesE5otgRmGS5ztQ+SSZSkDTEa/zd5+vPqvtSPwGoiiZ5t2yTPiLqW7JtxDsRJzUZZilkEHINETBgGLw/SOS6z/11RWkQFNfUMCiqoC8a7OVUqEiEMOtjRyPq6onml8j8BCnAgMBAAECgYEApx870p3ykYABgbfITZ3TT2L7McqytTg2w1rt9q3ojrYF4d7Na94X7uTB8Zqwj3EA3xdrCTcd+e6/B4f45n600ZrEggL85PgJ+padCifZMGRQVKyTWLXVujZEANj6h00DlBPA1aflX4lCuvr7jwTd+x/pPlkIOZEvccH3YdwrlKECQQDx3qy5EDDxUCTGzxnzt4E9/SfcwdXVaxME2/O9w4ZbJ04i7nH9JF5qqajMaohbSY7kQ1Dkbk9B1G1sUIgQGQEPAkEA1n5vaDx/QTr2fQ1M0XMs1fcAz5XqIyQBVyjCoy+WLQKsw880+55q9bAqstd3hgbCgRS5oGHGscVgGT/zYtxG6QJAf16T9qDgJTPfKhLFx+JK3SoDY+1vfgP08AokQTpF9pqsOxj2Tiho0CKoEX3IcMo7xVjtsF2mvzPuq6t8+30vYQJAczwADGccEuZjkHF5yLjw6I7ID75ByNlKIxJR5yvkqY/bU2NZ2qiUXCRl/CkIHKg7DPAefgypm84nP8l8r6saKQJAbcOdQzJ4DFfRAkPQ+pdPxDfxFLWrDVPdSG5yR1gmb1swNdQq88lvoSIS/qUpSUPZ0eb1g3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W5J528rgvs0s2g9ypuPSR1GO/FGjjfBnXqKRQKBgQC4E+VRDI+HzlH5e7gclSdiSHjSq+xzaD8m+SpNpZ2OJ4HsTVv9ZDOsWZ8KmC5DX2peKya9XEOG+SabIXEgG26TabfXJEQzaN2efR7LoS5QgaPPfEbQFshyrsyiKebggirLsuuWcNC47910MnhZEhEWus9fpbWb7Z+yvJAlCrD0QQKBgGs4I13sj5yOHeByXOkkrv0m8G3j7E98dbCWTL3d3G2RQNaoTchfEbBPgEFxV8mJS0PvmpJzA/3Wpjx9hzmi2TkfUSu0xXbIHVaXkG73haiEOgN+OTFcJax2ULnRzXWk8ABV2TUH/ZW4JH9zm3pD1g8F2SVcNEef2FR1rgTQH5nFAoGAU/HlXZ6y19vQUmtwjxR0nrPpQkapdSIynOlR3gSTwS1YuYPAmk0MG8j6SiaTPhTONeVekBZjS8dpR0cxoLQ3A6i947UCNjfLnt5pTRJ8j72n9kYrineqKY2gQ41+y1Tn6zQoJOin5NTPrQce/wLIbmVIifo8qlX/jzLRuJlNJgM=";
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
//        params.put("app_auth_token",req.getParameter("app_auth_token"));
        if(req.getParameter("pageType").equals("tradePay")){
            final JSONObject jsonObject = new JSONObject();
            jsonObject.put("pay_type",req.getParameter("pay_type"));
            jsonObject.put("mch_id",req.getParameter("mch_id"));
            jsonObject.put("sub_mchid",req.getParameter("sub_mchid"));
            jsonObject.put("official_mchid",req.getParameter("official_mchid"));
            jsonObject.put("auth_code",req.getParameter("auth_code"));
            jsonObject.put("total_amount",req.getParameter("total_amount"));
            jsonObject.put("out_trade_no",req.getParameter("out_trade_no"));
            jsonObject.put("extend_params",req.getParameter("extend_params"));
            jsonObject.put("goods_detail",req.getParameter("goods_detail"));
            jsonObject.put("goods_tag",req.getParameter("goods_tag"));
            jsonObject.put("attach", req.getParameter("attach"));
            jsonObject.put("subject",req.getParameter("subject"));
            params.put("biz_content",jsonObject.toString());
        }
        if(req.getParameter("pageType").equals("tradeJsPay")){
            params.put("pay_type",req.getParameter("pay_type"));
            params.put("mch_id",req.getParameter("mch_id"));
            params.put("sub_mchid",req.getParameter("sub_mchid"));
            params.put("official_mchid",req.getParameter("official_mchid"));
            params.put("total_amount",req.getParameter("total_amount"));
            params.put("out_trade_no",req.getParameter("out_trade_no"));
            params.put("extend_params",req.getParameter("extend_params"));
            params.put("goods_detail",req.getParameter("goods_detail"));
            params.put("goods_tag",req.getParameter("goods_tag"));
            params.put("attach", req.getParameter("attach"));
            params.put("subject",req.getParameter("subject"));
            params.put("notify_url",req.getParameter("notify_url"));
        }
        if(req.getParameter("pageType").equals("tradeQuery")||req.getParameter("pageType").equals("tradeClose")||req.getParameter("pageType").equals("tradeRevoke")){
            params.put("mch_id",req.getParameter("mch_id"));
            params.put("sub_mchid",req.getParameter("sub_mchid"));
            params.put("out_trade_no",req.getParameter("out_trade_no"));
            params.put("trade_no",req.getParameter("trade_no"));
        }
        if(req.getParameter("pageType").equals("tradeRefund")){
            params.put("mch_id",req.getParameter("mch_id"));
            params.put("sub_mchid",req.getParameter("sub_mchid"));
            params.put("out_trade_no",req.getParameter("out_trade_no"));
            params.put("trade_no",req.getParameter("trade_no"));
            params.put("out_refund_no",req.getParameter("out_refund_no"));
            params.put("refund_amount",req.getParameter("refund_amount"));
            params.put("refund_reason",req.getParameter("refund_reason"));
        }
        if(req.getParameter("pageType").equals("tradeRefundQuery")){
            params.put("mch_id",req.getParameter("mch_id"));
            params.put("sub_mchid",req.getParameter("sub_mchid"));
            params.put("out_trade_no",req.getParameter("out_trade_no"));
            params.put("trade_no",req.getParameter("trade_no"));
            params.put("out_refund_no",req.getParameter("out_refund_no"));
        }
        if(req.getParameter("pageType").equals("authOpenid")){
            params.put("redirect_url",req.getParameter("redirect_url"));
        }
        if(req.getParameter("pageType").equals("tradeDataDwnload")){
            params.put("bill_date",req.getParameter("bill_date"));
        }
        System.out.println("Sign-Params:"+params);
        final String sign_type = params.get("sign_type");
        params.remove("sign");
        params.remove("sign_type");
        String signStr = SignatureHelper.sign(params,"RSA".equalsIgnoreCase(sign_type)?privateKey:privayeKey2,sign_type);

        final boolean verify = SignatureHelper.verify(params, signStr, "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDKp5k9vyG2l99+06BrftMv4UfNelBG/Rdx1MAqkqnrBOaLYEZhkuc7UPkkmUpA0xGv83efrz6r7Uj8BqIomebdskz4i6luybcQ7ESc1GWYpZBByDREwYBi8P0jkus/9dUVpEBTX1DAoqqAvGuzlVKhIhDDrY0cj6uqJ5pfI/AQpwIDAQAB");
        System.out.println(verify);
        String signJson = "{\"signStr\":\""+signStr+"\"}";
        return signJson;
    }
}
